package com.empty.oredermenu.activity

import android.content.Intent
import android.os.Bundle
import android.util.Log
import com.empty.oredermenu.R
import com.empty.oredermenu.adapter.FindListAdapter
import com.empty.oredermenu.adapter.MenuDetaliLiatAdapter
import com.empty.oredermenu.base.OrderMenuBaseActivity
import com.empty.oredermenu.httpmannage.OkHttpMannager
import com.empty.oredermenu.httpmannage.OkHttp_CallResponse
import com.empty.oredermenu.javabeen.FindMenuListBeen
import com.empty.oredermenu.javabeen.MenuListAdapterBeen
import com.empty.oredermenu.utils.ToastUtils
import com.empty.oredermenu.utils.UrlAllUtils
import com.kexindai.kexinmodule.weight.pullrefreshlistview.XListView
import kotlinx.android.synthetic.main.layout_listview_activity.*
import okhttp3.Call
import okhttp3.Response
import org.json.JSONArray
import org.json.JSONObject
import java.util.*

/**
 * Created by empty cup on 2017/7/17.
 * 详情列表
 */

class MenuDetailListActivity : OrderMenuBaseActivity(),XListView.IXListViewListener, OkHttp_CallResponse {



    var index  = 1
    var menuId : String? = null//菜单分类ID
    var menuAdapter : MenuDetaliLiatAdapter ? = null
    var listbeen :ArrayList<MenuListAdapterBeen> = ArrayList();

    override fun OkHttp_ResponseSuccse(call: Call?, response: Response?, jsonObject: JSONObject?, tag: String?) {
        Log.v("order"," 数据 图片   "+jsonObject.toString())

        if (jsonObject!!.getString("code").equals("10000")){
            if (jsonObject!!.getString("data").equals("null")){
                ToastUtils.Toast_Long(this,"未找到此分类列表")
                return
            }
            var datajson : JSONArray = jsonObject.getJSONArray("data")
            //kotlin 闭区间写法
            for (i in 0 until datajson.length()){
                var json : JSONObject = datajson.getJSONObject(i)
                var menulistBeen :MenuListAdapterBeen = MenuListAdapterBeen()
                menulistBeen.description = json.getString("description")
                menulistBeen.food = json.getString("food")
                menulistBeen.id = json.getString("id")
                menulistBeen.keywords = json.getString("keywords")
                menulistBeen.img = json.getString("img")
                menulistBeen.name = json.getString("name")
                menulistBeen.imgs = json.getString("images")
                menulistBeen.msg = ""


                listbeen.add(menulistBeen)

            }


            if (listbeen.size/index >= 10){
                xlist_pull.setPullLoadEnable(true)
            }else{
                xlist_pull.setPullLoadEnable(false)
            }
            onLoad()
            menuAdapter!!.notifyDataSetChanged()
        }else{
            ToastUtils.Toast_Long(this,jsonObject!!.getString("message"))
        }
    }

    override fun OkHttp_CallonFailure(call: Call?) {
        onLoad()
    }

    override fun OkHttp_ErrorCode(code: String?, tag: String?) {
    }

    override fun user_NoLogin() {
    }

    override fun onLoadMore() {
        index ++
        Http_PageIndex(menuId,index)
    }

    override fun onRefresh() {
        index  = 1
        Http_PageIndex(menuId,index)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        toolsUtils!!.BuildColor(window, this, R.drawable.side_nav_bar)

        setContentView(R.layout.layout_listview_activity)

        baseInitView()
        layout_title!!.text = "菜谱列表"

        xlist_pull.setPullLoadEnable(false)
        xlist_pull.setPullRefreshEnable(true)
        xlist_pull.setXListViewListener(this)

        menuAdapter = MenuDetaliLiatAdapter(this@MenuDetailListActivity,listbeen)
        xlist_pull?.adapter = menuAdapter

        val intent = intent
        if (intent != null){
            menuId = intent.getStringExtra("menuid")
            Log.v("thisorder","     传入ID        "+menuId)
            Http_PageIndex(menuId,index)
        }
//        setFirstData()
        xlist_pull.setOnItemClickListener { _, _, position, _ ->
            if (listbeen.size>0&&(position-1)>=0){
                var intentmenu :Intent = Intent()
                intentmenu.setClass(this@MenuDetailListActivity,MenuDetailActivity::class.java)
                intentmenu.putExtra("listbeen",listbeen.get(position-1))
                startActivity(intentmenu)
            }
        }
    }

    /**
     * 网络访问方法
     */
    fun Http_PageIndex(id:String?,index : Int){
        val url: String =
                UrlAllUtils.URL_MENUDETAILE_ADDRESS +
                        "?key="+ UrlAllUtils.URL_MENUDETAILE_KEY+
                        "&rows=20"+"&id="+id+"&page="+index

        OkHttpMannager.getInstance().postAsynHttp(url,this@MenuDetailListActivity,this,false,"find")
    }

    private fun onLoad() {
        xlist_pull.stopRefresh()
        xlist_pull.stopLoadMore()
        xlist_pull.setRefreshTime2(ft.format(Date(System.currentTimeMillis())))
    }

    // 第一次加载数据
    fun setFirstData() {

        if (listbeen.size > 0) {
            return
        }

        if (listbeen.size <= 0) { // 未传入数据，自行进行刷新
            if (!xlist_pull.mPullRefreshing)
                xlist_pull.startHeaderRefresh()
        }
    }

}
